package com.wen.top.Utils;

import com.sun.istack.internal.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @ClassName PagedUtils
 * @Description list 的简单分页
 * @Author 黄文聪
 * @Date 2020/4/2 13:54
 * @Version 1.0
 **/
public class PagedUtils {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 51; i++) {
            list.add(i);
        }
        Pages paged = Paged(list,5,11);
        System.out.println(paged);
    }


    /**
     * 分页
     * @param list 总数据
     * @param pageSize 每页大小
     * @param pageNo 默认页数
     * @return
     */
    private static Pages Paged(@NotNull List<?> list, Integer pageSize, Integer pageNo){
        Pages pageVo= new Pages();
        Integer totals = list.size();
        //获取总页数
        Integer pages = totals % pageSize < 0 ? totals/pageSize : totals/pageSize +1;
        //判断pageNo 页数是否低于0或者大于总页数；
        pageNo = pageNo > pages ? pageNo = pages : pageNo;
        pageNo = pageNo <= 0 ? pageNo = 1 : pageNo;
        //判断每页大小是否超出总数据大小
        pageSize = pageSize > totals ? pageSize = totals : pageSize;
        //获取list切割初始地址
        int startIndex = pageSize *pageNo -pageSize;
        int endIndex = pageNo * pageSize<totals ?pageNo *pageSize :totals;

        List<?> subList = list.subList(startIndex, endIndex);
        pageVo.setList(subList);
        pageVo.setPageNo(pageNo);
        pageVo.setPages(pages);
        pageVo.setPageSize(pageSize);
        pageVo.setTotal(totals);
        return pageVo;

    }
    /**
     * 分页数据类
     */
    static class Pages{
        //完整数据
        private Collection list;
        //总条目
        private Integer total;
        //当前页码
        private Integer pageNo;
        //每页大小
        private Integer pageSize;
        //总页数
        private Integer pages;

        public Pages() {
        }

        public Pages(Collection list, Integer total, Integer pageNo, Integer pageSize, Integer pages) {
            this.list = list;
            this.total = total;
            this.pageNo = pageNo;
            this.pageSize = pageSize;
            this.pages = pages;
        }

        public Collection getList() {
            return list;
        }

        public void setList(Collection list) {
            this.list = list;
        }

        public Integer getTotal() {
            return total;
        }

        public void setTotal(Integer total) {
            this.total = total;
        }

        public Integer getPageNo() {
            return pageNo;
        }

        public void setPageNo(Integer pageNo) {
            this.pageNo = pageNo;
        }

        public Integer getPageSize() {
            return pageSize;
        }

        public void setPageSize(Integer pageSize) {
            this.pageSize = pageSize;
        }

        public Integer getPages() {
            return pages;
        }

        public void setPages(Integer pages) {
            this.pages = pages;
        }

        @Override
        public String toString() {
            return "Pages{" +
                    "第一页数据=" + list +
                    ", 总条目=" + total +
                    ", 当前页码=" + pageNo +
                    ", 每页显示=" + pageSize +
                    ", 总页数=" + pages +
                    '}';
        }
    }
}
